package ics.zychen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AccountListAdapter extends BaseAdapter{
	private LayoutInflater mInflater; 
	private List<HashMap<String, Object>> listData;
	private Map<Integer, Map<String, Object>> selectMap = new HashMap<Integer, Map<String, Object>>();
	Vector<Integer> sets;
	private class ViewHolder {  
        public ImageView icon;  
        public TextView name;  
        public TextView sns;
        public CheckBox checkBox;  
    } 
	
	public AccountListAdapter(Context context, List<HashMap<String, Object>> listData, Vector<Integer> sets) {  
        this.mInflater = LayoutInflater.from(context);  
        this.listData = listData;  
        this.sets = sets;
    }  
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;  
        if (convertView == null) {  
            holder = new ViewHolder();  
            convertView = mInflater.inflate(R.layout.account_list_item, null);  
            final View view = convertView;
            holder.icon = (ImageView) convertView.findViewById(R.id.acc_icon);  
            holder.name = (TextView) convertView.findViewById(R.id.acc_name);
            holder.sns = (TextView) convertView.findViewById(R.id.acc_sns_name);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.acc_check);  
            holder.checkBox.setOnClickListener(new OnClickListener() {  
                @Override
                public void onClick(View v) {  
                    if (selectMap.get(position) != null) {  
                        selectMap.remove(position);
                        sets.remove((Integer)position);
                    } else {  
                        selectMap.put(position, listData.get(position));
                        sets.add(position);
                    }  
                    Toast.makeText(view.getContext(),  
                            "你选择了:" + selectMap.size() + "个.",  
                            Toast.LENGTH_LONG).show();  
                }
            });
            convertView.setTag(holder);  
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
  
        holder.name.setText((CharSequence) listData.get(position).get("ItemText"));
        holder.sns.setText((CharSequence) listData.get(position).get("ItemText2"));
        holder.icon.setImageBitmap((Bitmap) listData.get(position).get("ItemImage"));
        if (selectMap.get(position) != null) {  
            holder.checkBox.setChecked(true);  
        } else {  
            holder.checkBox.setChecked(false);  
        }  
  
        return convertView;  
    }  
}